package com.neuedu.corejava.ch9.homework;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailClass {
   //public static boolean main(String[] args)
	public static void main(String[] args) {
		
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请你输入你的邮箱地址：");
		String emailString = sc.nextLine(); 
		
		if(emailString.indexOf("@")==-1 || emailString.indexOf(".")==-1) {
			System.out.println("1、输入的邮箱不合法没有包含@和.");
			//return false;
		}else if(emailString.indexOf("@") !=emailString.lastIndexOf("@") 
			  || emailString.indexOf(".") !=emailString.lastIndexOf(".")) {
			System.out.println("2、输入的邮箱包含多个@和.");
			//return false;
		}else if(emailString.indexOf("@") > emailString.indexOf(".")) {
			System.out.println("3、输入的邮箱中.出现在了@前面");
			//return false;
		}else if(!checkEmailContent(emailString)) {
			System.out.println("4、邮箱中包含其他字符,只能包含字母和数字");
			//return false;
		}else {
			System.out.println("有效的邮箱！");
		}
		
		 //邮箱正则表达式      ^[0-9a-z]+\w*@([0-9a-z]+\.)[0-9a-z]+$
	  	    String patteString="  ^[0-9a-z]+\\w*@([0-9a-z]+\\.)[0-9a-z]+$";
	  	    Pattern pattern=Pattern.compile(patteString);
	        Matcher matcher=pattern.matcher(emailString);
	        if(matcher.matches()){
	            System.out.println("有效邮箱");
	        }else {
	            System.out.println("无效邮箱");
	        }
		
		
		
		
		
	}
	
	private static boolean checkEmailContent(String email) {
		for(int i=0;i<email.length();i++) {
			char c=email.charAt(i);
			boolean flag = c>='A' && c<='Z' || c>='a' && c<='z' ||c=='@' || c=='.';
			if(flag) {
				continue;
			}else {
				return false;//只要有一个符号不满足，则邮箱不满足要求。
			}
			
		}
		return true;
	}

}
